import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//负责像ServletContext里面写数据
//例如通过/writer?message=aaa访问到WriterServlet，就把message=aaa这个键值对存到ServletContext
@WebServlet("/writer")
public class WriterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //1.从请求中获取到message参数
        String message = req.getParameter("message");
        //2.取出ServletContext对象（这个对象是Tomcat在加载webapp的时候自动创建的）
        ServletContext context = this.getServletContext();
        //3.往这里写入键值对
        context.setAttribute("message",message);
        //4.返回响应
        resp.getWriter().write("<h3>存储message成功</h3>");
    }
}
